package com.bdqn.systems.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.enums.RoleStatusEnum;
import com.bdqn.response.Result;
import com.bdqn.systems.entity.Role;
import com.bdqn.systems.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author cp
 * @since 2020-11-30
 */
@Api(value = "角色信息接口",tags = "访问角色信息接口")
@RestController
@RequestMapping("/systems/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "访问角色信息", notes = "可根据角色名称查询角色")
    @GetMapping("/findAll")
    public Result findByName(String roleName, Integer current, Integer size){
        Page<Role> rolePage = new Page<>(current, size);
        Page<Role> byName = roleService.findByName(roleName, rolePage);

        return Result.ok().data("roles",byName);
    }

    @ApiOperation(value = "编辑角色信息",notes = "获取角色信息用于修改")
    @GetMapping("/compile")
    public Result compile(Integer id){
        Role role = roleService.getBaseMapper().selectById(id);
        return Result.ok().data("role",role);
    }

    @ApiOperation(value = "修改角色信息",notes = "修改角色信息")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        int i = roleService.getBaseMapper().updateById(role);
        return i>0?Result.ok():Result.error();
    }

    @ApiOperation(value = "新增角色信息",notes = "新增角色信息")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role){
        int insert = roleService.insert(role);
        return insert>0?Result.ok():Result.error();
    }

    @ApiOperation(value = "删除角色",notes = "删除角色信息")
    @DeleteMapping("/delRole")
    public Result delRole(Integer id){
        return Result.ok();
    }

    @ApiOperation(value = "编辑角色状态",notes = "修改角色状态")
    @GetMapping("/editRoleState")
    public Result editRoleState(Integer id){
        boolean b = roleService.editRoleState(id);
        return b?Result.ok():Result.error();
    }


    @ApiOperation(value = "导出角色信息",notes = "根据要求导出角色信息")
    @PostMapping(value = "/export")
    public void exportRoleExcel(HttpServletResponse response,
                                  String roleName) throws IOException {
        List<Role> roleAll = roleService.findRoleAll(roleName);
        roleService.exportRoleExcel(response, roleAll);
    }
}

